﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.Business.Utility;
using System.Data;
using System.Text;
using DMS.Business.Modules.Organization;
using DMS.Business.Modules.SystemManage;
using System.Reflection;
using DMS.Business.Modules.SystemManagement;

namespace DMS.Web.Common.Services
{

    public partial class NoticeHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string methodName = Request.Params["method"];
            MethodInfo method = this.GetType().GetMethod(methodName);

            method.Invoke(this, null);
        }

        public void List()
        {
            string result = "{\"total\":\"@total\", \"content\":\"@content\", \"title\":\"@title\"}";
            int pageIndex = int.Parse(Request.Params["pageIndex"]);
            int total = NoticeService.CountMyNotice();
            if (total == 0)
            {
                result = result.Replace("@total", total.ToString()).Replace("@content", "");
            }
            else
            {
                DataSet ds = NoticeService.ListMyNotice("");
                DataTable dt = ds.Tables[0];
                DataRow dr = dt.Rows[pageIndex - 1];
                string title = Convert.ToString(dr["Title"]);
                string content = "";
                if(!dr.IsNull("Content"))
                    content = Convert.ToString(dr["Content"]);
                result = result.Replace("@total", total.ToString()).Replace("@content", content.Replace("\"", "'").Replace("\\","/").Replace("\r\n", "")).Replace("@title", title);
            }
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(result);
            Response.End();
        }

        public void GetNotice()
        {
            string id = Request.Params["id"];
            string result = "{\"total\":\"1\", \"content\":\"@content\", \"title\":\"@title\"}";
            Sys_notice notice = NoticeService.GetNotice(id);
            string content = "";
            if (!String.IsNullOrEmpty(notice.Content))
                content = notice.Content;
            result = result.Replace("@content", content.Replace("\"", "'").Replace("\\", "/").Replace("\r\n", "")).Replace("@title", notice.Title);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(result);
            Response.End();
        }

        public void Ignore()
        {
            NoticeService.IgnoreMyNotice();
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.End();
        }
    }
}